<?php
/**
 * @package     Joomla.Plugin
 * @subpackage  System.Admin Skin
 *
 * @copyright   Copyright (C) 2005 - 2014 Open Source Matters, Inc. All rights reserved.
 * @license     GNU General Public License version 2 or later; see LICENSE
 */

defined('_JEXEC') or die;

/**
 * Joomla! Admin skin plugin.
 *
 * @package     Joomla.Plugin
 * @subpackage  System.Debug
 * @since       1.5
 */
class PlgSystemAdminSkin extends JPlugin
{
	/**
	 * Load the language file on instantiation.
	 *
	 * @var    boolean
	 * @since  3.1
	 */
	protected $autoloadLanguage = true;

	/**
	 * Constructor.
	 *
	 * @param   object  &$subject  The object to observe.
	 * @param   array   $config    An optional associative array of configuration settings.
	 *
	 * @since   1.5
	 */
	public function __construct(&$subject, $config)
	{
		parent::__construct($subject, $config);
	}

	function onAfterRender()
	{
        //Initialise variables
		$app = JFactory::getApplication();

		//getting body code and storing as buffer
		$buffer = $app->getBody();

        //getting option
        $input  = $app->input;
        $option = $input->get('option', '', 'cmd');

        if(!$app->isAdmin() && $option =='com_ownservices')
        {
            // adding the skin class in the body tag and then replacing the buffer
            $buffer = preg_replace ("/<body class=\"contentpane modal\">/", "<body class=\"contentpane skin-blue\">", $buffer);

			 // overwrite an icon calendar of joomla core.
            $buffer = preg_replace ("/<i class=\"icon-calendar\">/", "<i class=\"fa fa-calendar\">", $buffer);

			// remove css of main template
            $buffer = preg_replace_callback('/<link(.*?)(type=)("|\')(text\/css)("|\')(.*?)\/>/i', array($this, 'recordCSS'), $buffer);

			// remove javascript of core.
			$buffer = preg_replace_callback('/<script(.*?)(src=)("|\')(.*?)("|\')(.*?)><\/script>/i', array($this, 'recordJS'), $buffer);
        }

		//output the buffer
		$app->setBody($buffer);

		return true;
	}


    function recordCSS($matches)
    {
		//Initialsize object
		$app	= JFactory::getApplication();
		$theme	= $app->getTemplate();
        $merge	= implode($matches);

        if (strstr($matches[0], '?')) return $matches[0];

        preg_match('/(.*?)href=("|\')(.*?)\.css("|\')/', $merge, $fl);

        if (@strlen($fl[3])<2) {
            return $matches[0];
        }

        $fl[3] = $fl[3].'.css';

        $ret = '';
        if($fl[3] == 'templates/'.$theme.'/css/template.css')
        {
            $ret .= '<!-- REMOVED CSS: '.$fl[3].' -->';
        }
        else
        {
            $ret .= '<link rel="stylesheet" href="'.$fl[3].'" type="text/css" />';
        }
        return $ret;
    }

	function recordJS($matches)
	{
		if (strstr($matches[0], '?')) return $matches[0]; 
	
		$merge = implode($matches);

		if (!strstr($merge, 'text/javascript')){
			return $matches[0];
		}

        preg_match('/(.*?)src=("|\')(.*?)("|\')(.*?)/', $merge, $fl);

		if ( (@strlen($fl[3])<2) ) {
			return $matches[0];
		}
        if (@strstr(strtolower($fl[3]), 'media/jui/js/jquery.min.js'))
        {
		    return '<!-- REMOVED JS: '.$fl[3].' -->';
        }
        else if(@strstr(strtolower($fl[3]), 'media/jui/js/jquery-noconflict.js'))
        {
            return '<!-- REMOVED JS: '.$fl[3].' -->';
        }
        else if(@strstr(strtolower($fl[3]), 'media/jui/js/jquery-migrate.min.js'))
        {
            return '<!-- REMOVED JS: '.$fl[3].' -->';
        }
        else if(@strstr(strtolower($fl[3]), 'media/jui/js/bootstrap.min.js'))
        {
            return '<!-- REMOVED JS: '.$fl[3].' -->';
        }
        else
        {
            return '<script src="'.$fl[3].'" type="text/javascript"></script>';
        }
	}
}
